/*********************************************

   How does random assignment to teams compare
   with baseline assignment in the firm. 

**********************************************/

clear all
set segmentsize 3g

set seed 136

* Load Randomixed Teams
use $Data/Original/Randomized_Teams.dta, clear

preserve
keep new_section team Line_R high_skilled 
drop if high_skilled 
sort Line_R new_section team, stable
gen _id = _n
tempfile temp_unskilled_positions
save `temp_unskilled_positions'
restore

keep name Religion high_skilled
drop if high_skilled 
gen rand = runiform()
sort rand, stable
gen _id = _n

merge 1:1 _id using `temp_unskilled_positions'

tempfile unskilled_randomized
save `unskilled_randomized'


* Load Randomized Teams
use $Data/Original/Randomized_Teams.dta, clear

preserve
keep Line_R new_section high_skilled Religion team
keep if high_skilled
sort Line_R new_section, stable
gen _id = _n
tempfile temp_skilled_positions
save `temp_skilled_positions'
restore 

keep name Religion high_skilled Line_R new_section
keep if high_skilled
sort Line_R new_section, stable 
gen rand = runiform()
sort Line_R new_section rand, stable
gen _id = _n

merge 1:1 _id using `temp_skilled_positions'


append using `unskilled_randomized'
sort Line_R new_section team 
rename new_section section


gen muslim = Religion == "Islam"
gen Total_workers = 1

keep if inlist(team, "A", "D") /* Keep one cohort to compare with actual allocations (there are a total of 4 teams across Lines 5 and 6, named by letters A to D.) */
collapse (mean) muslim (sum) Total_workers, by(Line section)
gen Muslim_workers = muslim*Total_workers
replace muslim = round(muslim, .001)

la var muslim "Share Muslim"
la var Muslim_workers "Muslim Workers"
la var Total_workers "Total Workers"

gen randomized = 1

* Append with baseline composition for one cohort
append using $Data/Original/Baseline_LS_composition.dta

twoway histogram muslim if randomized == 0, discrete barwidth(0.04) freq color(red%30) lcolor(red%60) lpattern(solid) || ///
       histogram muslim if randomized == 1, discrete barwidth(0.04) freq color(blue%30) lcolor(blue%60) lpattern(dash) ///
       legend(label(1 "Actual Shares") label(2 "Randomized Shares")) ytitle("Number of sections")

/* twoway histogram muslim if randomized == 0, discrete barwidth(0.04) freq color(red%30) || ///
       histogram muslim if randomized == 1, discrete barwidth(0.04) freq color(blue%30)  ///
       legend(label(1 "Actual Shares") label(2 "Randomized Shares")) ytitle("Number of sections")
*/

graph export $Output/Figures/allocation_comparisons.pdf, replace

